package com.myblog.service.impl;

import com.myblog.entity.Blog;
import com.myblog.mapper.BlogMapper;
import com.myblog.service.IBlogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 沉梦听雨
 * @since 2023-03-25
 */
@Service
public class IBlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IBlogService {

    @Autowired
    private BlogMapper blogMapper;
    @Override
    public IPage<Blog> getPage(int currentPage, int pageSize, Blog blog) {

        //用于Lambda语法使用的查询Wrapper 条件构造器
        LambdaQueryWrapper<Blog> lqw = new LambdaQueryWrapper<Blog>();
        lqw.like(Strings.isNotEmpty(blog.getTitle()), Blog::getTitle, blog.getTitle());
        lqw.orderByDesc(Blog::getCreated, Blog::getId);
        IPage page = new Page(currentPage, pageSize);
        blogMapper.selectPage(page, lqw);
        return page;
    }
}
